package com.lancer.volatileTest;


/**
 * 验证没有volatile时：线程A修改后，已经拿到线程的B不会拿到修改值，在修改之后才去拿数据的C，可以拿到修改后的值
 */
public class ViewAbleDemo2 {

    public static void main(String[] args) throws InterruptedException {
        MyData myData = new MyData();

        new Thread(()-> {
            System.out.println(Thread.currentThread().getName() + " come in");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            myData.add();
            System.out.println(Thread.currentThread().getName() + " change number value");
        },"AAA").start();


        new Thread(()-> {
            while(myData.number1 == 0){

            }
            System.out.println(Thread.currentThread().getName() + " number2 value is " + myData.number1);
        },"BBB").start();

        new Thread(()-> {
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " number2 value is " + myData.number1);
        },"CCC").start();
    }
}
